package com.project.service;

import com.alibaba.fastjson.JSONObject;
import com.project.dao.InternLogMapper;
import com.project.entity.InternInfo;
import com.project.entity.InternLog;
import com.project.entity.OrgInfo;
import com.project.entity.User;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hasee on 2017/4/28.
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class InternlogServiceImpl extends BaseService implements InternlogService {

    @Autowired
    private InternInfoService internInfoService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private UserService userService;


    private static InternLogMapper internLogMapper = null;

    public void setMapper() {
        if (internLogMapper == null)
            internLogMapper = sqlSessionTemplate.getMapper(InternLogMapper.class);//1.获取相关mapper
    }

    @Override
    public InternLog findById(int id) {
        setMapper();
        return internLogMapper.selectByPrimaryKey(id);
    }

    @Override
    public int update(InternLog internLog) {
        setMapper();
        return internLogMapper.updateByPrimaryKeySelective(internLog);
    }

    @Override
    public int add(InternLog internLog) {
        setMapper();
        return internLogMapper.insertSelective(internLog);
    }

    @Override
    public int delete(int id) {
        setMapper();
        return internLogMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<InternLog> findall() {
        setMapper();
        return internLogMapper.selectAll();
    }

    @Override
    public List<InternLog> findByCondition(InternLog internLog) {
        setMapper();
        return internLogMapper.selectByCondition(internLog);
    }

    /**
     * author: WheelChen
     * date: 2017/8/4
     * function: 根据实习日志一条记录查找实习日志详细信息
     *
     * @param internLog 实习日志
     * @return
     */
    @Override
    public JSONObject findDetailOneByInternLog(InternLog internLog) throws Exception {
        JSONObject jsonObject = new JSONObject();
        InternInfo internInfo;
        OrgInfo orgInfo;
        if (internLog.getEmployer() != null) {
            //首先获取实习信息表记录
            internInfo = internInfoService.findById(internLog.getEmployer());
            if (internInfo == null)
                throw new Exception("查找不到对应实习信息记录");
            else if (internInfo.getOrgid() != null) {
                //再获取企业信息
                orgInfo = orgInfoService.findByid(internInfo.getOrgid());
                jsonObject.put("orgInfo", orgInfo);
            }
        }
        jsonObject.put("internLog", internLog);
        return jsonObject;
    }

    /**
     * author: WheelChen
     * date: 2017/8/5
     * function: 根据internAdvisor查找实习日志详情（即查看该老师指导的学生的实习日志） 根据实习日志条件查找
     *
     * @param internLog           实习日志
     * @param internAdvisorUserId 校内指导老师用户表id
     * @param pageSize            分页大小
     * @param pageIndex           页码
     * @return
     */
    @Override
    public JSONObject findDetailBatchByInternAdvisorCondition(InternLog internLog, int internAdvisorUserId,
                                                              int pageSize, int pageIndex) throws Exception {
        JSONObject jsonObject2 = new JSONObject();
        List<JSONObject> jsonObjectList = new ArrayList<>();

        if (internLog == null)
            internLog = new InternLog();
        List<User> userList = new ArrayList<>();
        //根据校内指导老师用户表id查出符合条件的学生用户表记录
        if (internAdvisorUserId != -1)
            userList = studentService.findBatchByInternAdvisorUserId(internAdvisorUserId);

        //获取id
        List<Integer> ids = new ArrayList<>();
        if (userList != null)
            for (User user : userList) {
                ids.add(user.getId());
            }

        internLog.setIds(ids);
        internLog.setPageSize(pageSize);
        internLog.setPageIndex(pageIndex);
        //先查出符合条件的日志记录
        List<InternLog> internLogList = findByCondition(internLog);
        //根据日志记录查出符合条件的详情
        for (InternLog internLog1 : internLogList) {
            //企业信息和实习日志信息
            JSONObject jsonObject = findDetailOneByInternLog(internLog1);
            //用户表信息
            User user = userService.findByUserIdUnDecry(internLog1.getComposerid());
            filterSensitiveInfo(user);
            jsonObject.put("user", user);
            jsonObjectList.add(jsonObject);
        }
        jsonObject2.put("total", internLog.getTotal());
        jsonObject2.put("data", jsonObjectList);
        return jsonObject2;
    }

    /**
     * author: WheelChen
     * date: 2017/9/19
     * function: 根据用户表idList批量删除
     *
     * @param userIdList
     * @return
     */
    @Override
    public int removeBatchByUserIdList(List userIdList) {
        setMapper();
        return internLogMapper.deleteBatchByUserIdList(userIdList);
    }
}
